package com.wzm.basic.mq;

import com.wzm.order.domain.OrderDto;
import com.wzm.order.service.IOrderPetAcquisitionService;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.springframework.beans.factory.annotation.Autowired;

import java.nio.charset.StandardCharsets;

public class Producer {
    @Autowired
    private static IOrderPetAcquisitionService orderPetAcquisitionService;
    public static void producer(OrderDto orderDto) throws Exception {
        // 1.创建消费者对象
        DefaultMQProducer producer = new DefaultMQProducer("pet_order");
        // 2.连接mq
        producer.setNamesrvAddr("127.0.0.1:9876");
        // 3.启动消费者
        producer.start();
        Message message = null;
        // 4.发送延迟消息
        if(orderDto == null){
            message = new Message("order", "pet", "订单过期".getBytes(StandardCharsets.UTF_8));
            // 4.1.设置延迟消息级别
            message.setDelayTimeLevel(18);
        }
        message = new Message("order", "pet", "订单确认成功".getBytes(StandardCharsets.UTF_8));
        orderPetAcquisitionService.confirm(orderDto);
        SendResult sendResult = producer.send(message);
        System.out.println(sendResult);
        // 5.关闭消费者
        producer.shutdown();
    }
}